package com.erp.module.crm.xXCrm

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.util.Log
import android.view.View
import android.widget.AdapterView
import androidx.appcompat.app.AlertDialog
import com.erp.Interface.OnUserDataInterface
import com.erp.R
import com.erp.activity.DanjianivActivity
import com.erp.activity.DanjuWTKActivity
import com.erp.bean.ProdyctionClientBean
import com.erp.mclass.QuanXian
import com.erp.module.crm.xXCrm.baoMingXi.BaomingxiKActivity
import com.erp.module.crm.xXCrm.guiMingXi.GuimingxiActivity
import com.erp.module.crm.xXCrm.wuLiao.WuliaoKActivity
import com.erp.myAdapter.GaugeivGvAdapter
import com.erp.myFragment.BaseKFragment
import com.erp.util.Httputil
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.fragment_clientfive.*

/**
 * Created by Administrator on 2016/11/2 0002.
 * 生产单  70409383
 */
class FragmentClientfive : BaseKFragment, View.OnClickListener {
    constructor(){
    }
    private val TAG = "FragmentClientfive"
    private var id: String = ""
    lateinit var dialog: AlertDialog
    private var mwidth: Int = 0
    private var session_name: String? = null
    private var isdingdan = false
    private var mcontext: Context? = null
    private var isgui = true
    private var isbao = true
    private var iswuliao = true
    private var istrue = true
    private var select = 0
    lateinit var all: String
    private val f = "FragmentClientfive"
    private var string: String = ""
    override fun getLayoutId(): Int {
        fragmentname = f
        return R.layout.fragment_clientfive
    }

    override fun initData() {
        mwidth = sp.getInt("width", 0)
        session_name = sp.getString("session_name", "")
        llJiaodian.isFocusable = true
        llJiaodian.isFocusableInTouchMode = true
        llJiaodian.requestFocus()
        llJiaodian.requestFocusFromTouch()
        if (!QuanXian.isviewquality && !QuanXian.iseditquality) {
            llPhoto.visibility = View.GONE
        }
        if (!QuanXian.isvieworder && !QuanXian.iseditorder) {
            llOrderPhoto.visibility = View.GONE
        }
        initClick()
        if (isdingdan) {
            dialog = Myutil.showupdatedialog(activity, "正在加载", false)
            setHttpProdyction()
        } else {
            notifydata(string)
        }
    }

    fun setdata(da: String) {
        isdingdan = false
        this.string = da
    }

    private fun initClick() {
        tvKehu.setOnClickListener(this)
        llGuimingxi.setOnClickListener(this)
        llBaomingxi.setOnClickListener(this)
        llWuliao.setOnClickListener(this)
        llPhoto.setOnClickListener(this)
        llOrderPhoto.setOnClickListener(this)
        gvGaugeImg.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
            val intent = Intent(activity, DanjianivActivity::class.java)
            intent.putExtra("url", prodyctionClientBean.deliver_voucher[position])
            startActivity(intent)
        }
        gvDeliverList.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
            val intent = Intent(activity, DanjianivActivity::class.java)
            intent.putExtra("url", prodyctionClientBean.deliver_list[position])
            startActivity(intent)
        }

    }

    fun notifydata(data: String) {
        if (data == "{}" || data == "") {
            ivGmx.setImageResource(R.mipmap.erp_gmx2)
            ivBmx.setImageResource(R.mipmap.erp_bmx2)
            ivWlmx.setImageResource(R.mipmap.erp_wlmx2)
            isgui = false
            isbao = false
            iswuliao = false
            istrue = false
            llOrderPhoto.visibility = View.GONE
            llPhoto.visibility = View.GONE
            prodyctionClientBean = ProdyctionClientBean()
        } else {
            prodyctionClientBean = JsonUtil.JsonProdyction(data)
            setTextProdyction(prodyctionClientBean)
            setistrue()
            if (isdingdan) {
                dialog.dismiss()
            }
            if (select == 4) {
                if (isgui && istrue) {
                    val intent = Intent(activity, GuimingxiActivity::class.java)
                    intent.putExtra("oid", prodyctionClientBean.id)
                    intent.putExtra("shengchan", prodyctionClientBean.onum)
                    startActivity(intent)
                } else {
                    Myutil.MyTost("没有柜明细数据")
                }
            }
            if (select == 5) {
                if (iswuliao && istrue) {
                    val intent2 = Intent(activity, WuliaoKActivity::class.java)
                    intent2.putExtra("shengchan", prodyctionClientBean.onum)
                    intent2.putExtra("all", all)
                    startActivity(intent2)
                } else {
                    Myutil.MyTost("没有物料数据")
                }
            }
            if (istrue && select == 3) {
                scll.scrollTo(0, 2000)
            }
        }
    }

    private fun setistrue() {
        if (prodyctionClientBean.guimingx == null) {
            ivGmx.setImageResource(R.mipmap.erp_gmx2)
            isgui = false
        } else {
            if (prodyctionClientBean.guimingx.size == 0) {
                ivGmx.setImageResource(R.mipmap.erp_gmx2)
                isgui = false
            } else {
                ivGmx.setImageResource(R.mipmap.erp_gmx)
                tvGuimingxi.setTextColor(Color.parseColor("#0071B3"))
                isgui = true
            }
        }
        if (prodyctionClientBean.baomingxi == null) {
            ivBmx.setImageResource(R.mipmap.erp_bmx2)
            isbao = false
        } else {
            if (prodyctionClientBean.baomingxi.size == 0) {
                ivBmx.setImageResource(R.mipmap.erp_bmx2)
                isbao = false
            } else {
                ivBmx.setImageResource(R.mipmap.erp_bmx)
                tvBaomingxi.setTextColor(Color.parseColor("#0071B3"))
                isbao = true
            }
        }
        if (prodyctionClientBean.wuliaomingxis == null) {
            ivWlmx.setImageResource(R.mipmap.erp_wlmx2)
            iswuliao = false
        } else {
            if (prodyctionClientBean.wuliaomingxis.size == 0) {
                ivWlmx.setImageResource(R.mipmap.erp_wlmx2)
                iswuliao = false
            } else {
                ivWlmx.setImageResource(R.mipmap.erp_wlmx)
                iswuliao = true
                tvWuliao.setTextColor(Color.parseColor("#0071B3"))
            }
        }
    }

    private fun setHttpProdyction() {
        Httputil.getHttpProdyctionClient(mcontext, id, object : OnUserDataInterface {
            override fun onUserData(data: String) = notifydata(data)

            override fun onNot(throwable: String) {
                if (isdingdan) {
                    dialog.dismiss()
                }
                llGuimingxi.visibility = View.GONE
                llBaomingxi.visibility = View.GONE
                llWuliao.visibility = View.GONE
            }
        })
    }

    lateinit var gvGaugeImggvaapter: GaugeivGvAdapter
    lateinit var gvDeliverListgvaapter: GaugeivGvAdapter
    private fun setTextProdyction(prodyctionClientBean: ProdyctionClientBean) {
        Myutil.settv(prodyctionClientBean.content_description, tvProductionContentDescription)
        if (prodyctionClientBean.special_status == "5") {
            Myutil.settv("已取消", tvProductionSpecialStatus)
        }
        if (prodyctionClientBean.deliver_voucher.size != 0) {
            Log.d("photo","deliver_voucher="+prodyctionClientBean.deliver_voucher.toString())
            gvGaugeImggvaapter = GaugeivGvAdapter(prodyctionClientBean.deliver_voucher, this.activity, mwidth, session_name)
            gvGaugeImg.adapter = gvGaugeImggvaapter
        } else {
            Myutil.settv("暂无", tvDeliverVoucher)
        }
        if (prodyctionClientBean.deliver_list.size != 0) {
            Log.d("photo","deliver_list="+prodyctionClientBean.deliver_list.toString())
            gvDeliverListgvaapter = GaugeivGvAdapter(prodyctionClientBean.deliver_list, this.activity, mwidth, session_name)
            gvDeliverList.adapter = gvDeliverListgvaapter
        } else {
            Myutil.settv("暂无", tvDeliverList)
        }
        Myutil.settv(prodyctionClientBean.customer, tvKehu)
        Myutil.settv(prodyctionClientBean.cabinet_counts, tvCabinetCounts)
        Myutil.settv(prodyctionClientBean.all_package_num, tvAllPackageNum)
        Myutil.settv(prodyctionClientBean.s_package_num, tvSPackageNum)
        Myutil.settv(prodyctionClientBean.m_package_num, tvMPackageNum)
        Myutil.settv(prodyctionClientBean.rs_package_num, tvRsPackageNum)
        Myutil.settv(prodyctionClientBean.shop_name, tvProductionShopName)
        Myutil.settv(prodyctionClientBean.ot_name, tvProductionOtName)
        Myutil.settv(prodyctionClientBean.warehoused_num, tvWarehousedNum)
        Myutil.settv(prodyctionClientBean.delivered_num, tvDeliveredNum)
        Myutil.settv(prodyctionClientBean.onum, tvProductionOnum)
        Myutil.settv(prodyctionClientBean.contract_num, tvProductionContractNum)
        Myutil.settv(prodyctionClientBean.sqxd_uid, tvProductionSqxdUid)
        Myutil.settv(prodyctionClientBean.auditor, tvProductionAuditor)
        Myutil.settv(Myutil.getdata(prodyctionClientBean.sqxd_time), tvProductionSqxdTime)
        Myutil.settv(prodyctionClientBean.quoted_price_yuan_name, tvBaojiayuan)
        Myutil.settv(Myutil.getdata(prodyctionClientBean.quoted_price_time), tvBaojiayuanTime)
        Myutil.settv(Myutil.getdata(prodyctionClientBean.audit_time), tvProductionAuditTime)
        Myutil.settv(prodyctionClientBean.charge_auditor, tvProductionChargeAuditor)
        Myutil.settv(Myutil.getdata(prodyctionClientBean.charge_audit_time), tvProductionChargeAuditTime)
        Myutil.settv(Myutil.getdata(prodyctionClientBean.warehousing_time), tvProductionWarehousingTime)
        Myutil.settv(Myutil.getdata(prodyctionClientBean.deliver_time), tvProductionDeliverTime)
        Myutil.settv(Myutil.getdata(prodyctionClientBean.ctime), tvProductionCtime)
        Myutil.settv(prodyctionClientBean.remark, tvProductionRemark)
        Myutil.settv(Myutil.getdata(prodyctionClientBean.productive_time), tvProductionProductiveTime)
        if (prodyctionClientBean.crm_oid != "null" && prodyctionClientBean.crm_oid != null && prodyctionClientBean.crm_oid != "" && isdingdan) {
            tvKehu.setTextColor(Color.parseColor("#0071B3"))
        }
    }

    /**
     * @param id 当前生产单的id
     */
    fun godata(id: String, isdingdan: Boolean, context: Context, select: Int, all: String) {
        isVisibl = true;
        this.select = select
        this.isdingdan = isdingdan
        this.id = id
        this.mcontext = context
        this.all = all
    }


    override fun onClick(v: View) {
        Log.d("TAAAG", "oid=" + prodyctionClientBean.crm_oid);
        when (v) {
            tvKehu -> if (prodyctionClientBean.crm_oid != "null" && prodyctionClientBean.crm_oid != null && prodyctionClientBean.crm_oid != "" && isdingdan) {
                val intent = Intent(activity, ClientKActivity::class.java)
                intent.putExtra("id", prodyctionClientBean.crm_oid)
                startActivity(intent)
                val activity = mcontext as Activity?
                activity!!.finish()
            }
            llGuimingxi -> if (isgui && istrue) {
                val intent = Intent(activity, GuimingxiActivity::class.java)
                intent.putExtra("shengchan", prodyctionClientBean.onum)
                intent.putExtra("oid", prodyctionClientBean.id)
                startActivity(intent)
            }
            llBaomingxi -> if (isbao && istrue) {
                val intent1 = Intent(activity, BaomingxiKActivity::class.java)
                intent1.putExtra("shengchan", prodyctionClientBean.onum)
                startActivity(intent1)
            }
            llWuliao -> if (iswuliao && istrue) {
                val intent2 = Intent(activity, WuliaoKActivity::class.java)
                intent2.putExtra("shengchan", prodyctionClientBean.onum)
                intent2.putExtra("all", "not")
                startActivity(intent2)
            }
            llPhoto -> {
                Log.d("DanjuWTKActivity","oid="+ prodyctionClientBean.oid)
                val intent = Intent(activity, DanjuWTKActivity::class.java)
                intent.putExtra("oid", prodyctionClientBean.oid)
                intent.putExtra("shop_name", prodyctionClientBean.shop_name)
                intent.putExtra("contract_num", prodyctionClientBean.contract_num)
                intent.putExtra("onum", prodyctionClientBean.onum)
                intent.putExtra("code", 1)
                startActivity(intent)
            }
            llOrderPhoto -> {
                Log.d("DanjuWTKActivity","oid="+ prodyctionClientBean.oid)
                val intent2 = Intent(activity, DanjuWTKActivity::class.java)
                intent2.putExtra("oid", prodyctionClientBean.oid)
                intent2.putExtra("shop_name", prodyctionClientBean.shop_name)
                intent2.putExtra("contract_num", prodyctionClientBean.contract_num)
                intent2.putExtra("onum", prodyctionClientBean.onum)
                intent2.putExtra("code", 2)
                startActivity(intent2)
            }
        }
    }


    companion object {
        var prodyctionClientBean = ProdyctionClientBean()

        fun getwuliaomingxi(): ArrayList<ProdyctionClientBean.wuliaomingxi> =
                prodyctionClientBean.wuliaomingxis

        fun getbaomingxi(): ArrayList<ProdyctionClientBean.baomingxi> =
                prodyctionClientBean.baomingxi

        fun getguimingxi(): ArrayList<ProdyctionClientBean.guimingxi> =
                prodyctionClientBean.guimingx
    }
}
